﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

using My.IdlConverter.Com;
using My.IdlConverter.Core;
using My.IdlConverter.Core.Output;
using My.IdlConverter.Core.Cfg;
using My.IdlConverter.Core.Filters;

namespace My.IdlConverter.Winshell
{
    public partial class WinscriptForm : Form
    {
        public WinscriptForm()
        {
            InitializeComponent();
            OnInit();
        }

        public void OnInit()
        {
            txtConfigFile.Focus();
        }

        private void btnBrowse1_Click(object sender, EventArgs e)
        {
            SelectFile(txtConfigFile,"select configuration file");
        }

        private void btnBrowse3_Click(object sender, EventArgs e)
        {
            SelectFolder(txtTargetIdlFolder, "Please select the idl export folder.");
        }

        private void btnBrowse4_Click(object sender, EventArgs e)
        {
            SelectFolder(txtTargetClassFolder, "Please select classes export folder.");
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            Configuration.Load(txtConfigFile.Text);
            InterfaceModifier.ActiveRule = new ComFilterRule();
            MethodCollisionManager.Instance.RegisterCollisition(typeof(ComPropertiesCollision));
            MethodCollisionManager.Instance.RegisterCollisition(typeof(ComOverloadMethodCollision));

            try
            {
                //export idl file
                string idlFileName = txtIdlFileName.Text;
                if (idlFileName.IndexOf(".idl", StringComparison.OrdinalIgnoreCase) == -1)
                {
                    idlFileName += ".idl";
                }
                string targetPath = Path.Combine(txtTargetIdlFolder.Text, idlFileName);
                IExport idlExport = new ComIdlExporter(targetPath);
                idlExport.ExportEventHandler += OnExporting;
                idlExport.Export();

                //export classes
                IExport classExport = new ComClassExport(txtTargetClassFolder.Text);
                classExport.ExportEventHandler += OnExporting;
                classExport.Export();

                IExport regExport = new ComRegistarExport(txtTargetClassFolder.Text);
                regExport.ExportEventHandler += OnExporting;
                regExport.Export();

                OnAfterExport();
            }
            catch (Exception uie)
            {
                MessageBox.Show("Some exception occured when exporting the idl file.");
                Trace.Write(uie.StackTrace);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SelectFolder(TextBox txtTarget, string description)
        {
            txtTarget.Clear();
            folderBrowserDlg.RootFolder = Environment.SpecialFolder.MyComputer;
            folderBrowserDlg.Description = description;
            folderBrowserDlg.ShowNewFolderButton = true;
            DialogResult ret = folderBrowserDlg.ShowDialog();
            if (ret == DialogResult.OK)
            {
                txtTarget.Text = folderBrowserDlg.SelectedPath;
            }
        }

        private void SelectFile(TextBox txtTarget,string title)
        {
            txtTarget.Clear();
            fileBrowserDlg.Multiselect = false;
            fileBrowserDlg.Title = title;
            DialogResult ret = fileBrowserDlg.ShowDialog();
            if (ret == DialogResult.OK)
            {
                txtTarget.Text = fileBrowserDlg.FileName;
            }
        }

        private void OnAfterExport()
        {
            lblExportInfo.Text = "";
            if (chkOpenClassFolder.Checked)
            {
                Process.Start("explorer", txtTargetClassFolder.Text);
            }
            if (chkOpenInterfaceFolder.Checked)
            {
                Process.Start("explorer",txtTargetIdlFolder.Text);
            }
            if (chkExit.Checked)
            {
                this.Close();
            }
            ComGuidCenter.Save();
        }

        private void OnExporting(ExportEventArgs args)
        {
            string info = String.Format("Export {0}",args.FileName);
            lblExportInfo.Text = info;
            lblExportInfo.Update();
        }

        private void txtConfigFile_Validating(object sender, CancelEventArgs e)
        {
        }
    }
}